package com.qingfeng.coupon.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qingfeng.base.aop.LogInfo;
import com.qingfeng.client.api.BaseV1Api;
import com.qingfeng.client.bean.Seller;
import com.qingfeng.client.util.MakeOrderNum;
import com.qingfeng.coupon.bean.CouponTemplate;
import com.qingfeng.coupon.dao.CouponTemplateMapper;
import com.qingfeng.coupon.service.CouponTemplateService;
import com.qingfeng.coupon.vo.CouponTemplateQuery;
import com.qingfeng.coupon.vo.CouponTemplateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;


/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/5/26 2:40 下午
 */
@Api(tags = "优惠券模版管理")
@Slf4j
@RestController
@RequestMapping("/couponTemplate/")
public class CouponTemplateApi extends BaseV1Api {


    @Autowired
    private CouponTemplateMapper couponTemplateMapper;


    @Autowired
    private CouponTemplateService templateService;


    @ApiOperation("查询优惠券模版")
    @PostMapping("list")
    public Object query(@RequestBody CouponTemplateQuery pageQuery){
        Seller seller = getUser();
        pageQuery.getCondition().setStoreId(seller.getStoreId());
        if (pageQuery.getCondition().getState() != null) {
            switch (pageQuery.getCondition().getState()) {
                case 1:
                    pageQuery.getCondition().setAvailable(false);
                    break;
                case 2:
                    pageQuery.getCondition().setAvailable(true);
                    pageQuery.getCondition().setExpired(false);
                    pageQuery.getCondition().setFinished(false);
                    break;
                case 3:
                    pageQuery.getCondition().setAvailable(true);
                    pageQuery.getCondition().setExpired(true);
                    break;
                case 4:
                    pageQuery.getCondition().setFinished(true);
                    break;
            }
        }
        couponTemplateMapper.updateExpired();
        IPage<CouponTemplate> ipage = couponTemplateMapper.selectAll(pageQuery.getPage(),pageQuery.getCondition());
        ipage.getRecords().forEach(item->{
            if (item.getAvailable()==false){
                item.setStatus(1);
            }else if (item.getAvailable() && item.getExpired()==false
            && (item.getCount()>0 && item.getDistributeEnd().after(new Date()))){
                item.setStatus(2);
            }
            else if (item.getAvailable()&&item.getExpired()){
                item.setStatus(3);
            }else if (item.getCount()<=0 && item.getDistributeEnd().before(new Date())){
                item.setStatus(4);
            }
        });
        return successPage(ipage);
    }


    /**
     * 校验数据正确性
     * 调用保存方法
     * @param couponTemplateVo
     * @return
     */
    @ApiOperation("新增优惠券模版")
    @PostMapping("save")
    public Object save(@RequestBody CouponTemplateVo couponTemplateVo){

        couponTemplateVo.vaildata();

        CouponTemplate couponTemplate = couponTemplateVo.toCouponTemplate();

        //初始化部分数据,默认不可用
        Seller seller = getUser();
        couponTemplate.setCreateTime(new Date());
        couponTemplate.setStoreId(seller.getStoreId());
        couponTemplate.setCode(MakeOrderNum.getOrderNum());
        couponTemplate.setUserId(seller.getId());

        templateService.save(couponTemplate);

        return success("保存成功");
    }


    /**
     * 更新优惠券模版
     * @param couponTemplate
     * @return
     */
    @LogInfo(param = "修改优惠券模版")
    @ApiOperation("修改优惠券模版")
    @PostMapping("update")
    public Object update(@RequestBody CouponTemplateVo couponTemplateVo){
        couponTemplateVo.vaildata();

        CouponTemplate couponTemplate = couponTemplateVo.toCouponTemplate2();

        templateService.update(couponTemplate);
        return success("更新成功");
    }

    @ApiOperation("获取优惠券模版详情")
    @RequestMapping("get/{id}")
    @ApiImplicitParam(name = "id",value = "优惠券模版id",dataType = "Integer",required = true)
    public Object get(@PathVariable("id") Long id){
        CouponTemplate template = templateService.getDetail(id);
        return success(template);
    }

    @ApiOperation("修改分发状态")
    @GetMapping("updateAvailable/{id}/{available}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "优惠券模版id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "available", value = "是否可以发放", dataType = "Boolean", required = true)
    })
    public Object updateAvailable(@PathVariable("id") Long id,@PathVariable("available") Boolean available){
        CouponTemplate template = templateService.getDetail(id);
        template.setAvailable(available);
        couponTemplateMapper.updateById(template);
        return success(template);
    }




}
